<?php
// ----------------------------------------------------------
// * @Copyright @ 2011-2020 By HuaXiaXinCai
// ----------------------------------------------------------
// * @Author Yang Changwu<800334@qq.com> 2016-12-12
// ----------------------------------------------------------
namespace Controller;
use \GatewayWorker\Lib\Gateway;
use \Controller\Core;

class Game extends Core{
    //获取当前期次
    public function Period($data,$client_id) {
        //查询当前期次
        $period = $this->db->select('*')->from('sz_period')->orderByASC(array('id'),false)->row();
        $bettime = $period['endtime']-time()-$period['awardtime']-$period['exchangetime'];
        if($bettime>0){
            $msg = [
                'method' => 'Period',
                'timetype' => 'bet',
                'periodid' => $period['periodid'],
                'backtime' => $bettime,
            ];
            self::SendMsg(1001,$msg);
        }else{
            $awardtime = $period['endtime']-time()-$period['awardtime'];
            if($awardtime >0){
                $msg = [
                    'method' => 'Period',
                    'timetype' => 'award',
                    'periodid' => $period['periodid'],
                    'backtime' => $awardtime,
                ];
                self::SendMsg(1001,$msg);
            }else{
                $exchangetime = $period['endtime']-time();
                if($exchangetime > 0){
                    $msg = [
                        'method' => 'Period',
                        'timetype' => 'exchange',
                        'periodid' => $period['periodid'],
                        'periodid' => $period['periodid'],
                        'awardnumber' => $period['awardnumber'],
                        'backtime' => $exchangetime,
                    ];
                    self::SendMsg(1001,$msg);
                }
            }
        }
    }

    //历史开奖列表
    public function WinList($data,$client_id){
        if(!isset($data['page'])) $data['page'] = 1;
        if(!isset($data['pagesize'])) $data['pagesize'] = 10;
        $period = $this->db->select('periodid,begintime,endtime,awardnumber')->from('sz_period')->where('status=1 or status=2')->setPaging($data['pagesize'])->page($data['page'])->orderByASC(array('id'),false)->query();
        $periodcount = $this->db->select('count(*) as count')->from('sz_period')->where('status=1')->row();
        $msg = [
            'method' => 'WinList',
            'count' => $periodcount['count'],
            'page' => $data['page'],
            'data' => $period,
        ];
        self::SendMsg(1001,$msg);
    }

    //投注
    public function Bet($data,$client_id){
        if($_SESSION['username'] != $data['username']){
            return self::SendMsg(1007,array('methtod'=>'Bet'));
        }
        if($data['periodid']){
            $period = self::GetPeriod((int)$data['periodid']);
            if($period){
                $bettime = $period['endtime']-time()-$this->config['awardtime']-$this->config['exchangetime'];
                if($bettime<0){
                    return self::SendMsg(1016,array('methtod'=>'Bet'));
                }
            }else{
                return self::SendMsg(1015,array('methtod'=>'Bet'));
            }
        }else{
            return self::SendMsg(1015,array('methtod'=>'Bet'));
        }
        if(!$data['money'] || (int)$data['money']<=0){
            return self::SendMsg(1017,array('methtod'=>'Bet'));
        }
        if(!$data['castcodes']){
            return self::SendMsg(1018,array('methtod'=>'Bet'));
        }else{
            $res = self::CheckCast($data['castcodes'],$data['money']);
            if($res){
                return self::SendMsg($res,array('methtod'=>'Bet'));
            }
        }
        //判断金币
        $user = $this->db->select('gold')->from('sz_user')->where("username='".$_SESSION['username']."'")->row();
        if(($user['gold']-$data['money']) < 0){
            return self::SendMsg(1023,array('method'=>'Bet'));
        }
        
        $bet = [
            'orderid' => date(Ymd).self::RandString(1,4).date(His).self::RandString(1,4).self::RandString(3,1),
            'gameid' => 1,
            'username' => addslashes(trim($data['username'])),
            'periodid' => $data['periodid'],
            'money' => $data['money'],
            'castcodes' => addslashes(trim($data['castcodes'])),
            'betstatus' => 1,
            //'winmoney' => $data['winmoney'],
            //'awardstatus' => $data['awardstatus'],
            //'awardnumber' => $data['awardnumber'],
        ];
        $orderid = $this->db->select('*')->from('sz_betorder')->where("orderid='".$bet['orderid']."'")->row();
        if($orderid){
            $bet['orderid'] = date(Ymd).self::RandString(1,4).date(His).self::RandString(1,4).self::RandString(3,1);
            $orderid_two = $this->db->select('*')->from('sz_betorder')->where("orderid='".$bet['orderid']."'")->row();
            if($orderid_two){
                return self::SendMsg(1014,array('method'=>'Bet'));
            }
        }
        $this->db->insert('sz_betorder')->cols($bet)->query();
        //减掉投注金额
        $gold = $data['money'];
        $sqluser = "UPDATE `sz_user` SET `gold` = gold-$gold WHERE username='".$_SESSION['username']."'";
        $this->db->query($sqluser);
        //更新投注金额
        $money = $data['money'];
        $sql = "UPDATE `sz_period` SET `betsum` = betsum+$money WHERE periodid='".$bet['periodid']."'";
        $this->db->query($sql);
        //更新奖池金
        $sqlpool = "UPDATE `sz_config` SET `awardpool` = awardpool+$money WHERE channel='3721zh'";
        $this->db->query($sqlpool);
        //计算投注串
        self::CastCount($data['castcodes']);
        $awardtime = $period['endtime']-time();
        self::SendMsg(1001,array('method'=>'Bet','orderid'=>$bet['orderid'],'periodid'=>$bet['periodid'],'username'=>$data['username'],'backtime'=>$awardtime));
    }
    //投注记录
    public function BetRecord($data,$client_id){
        if(!isset($data['page'])) $data['page'] = 1;
        if(!isset($data['pagesize'])) $data['pagesize'] = 10;
        $order = $this->db->select('orderid,periodid,money,castcodes,betstatus,winmoney,awardstatus,awardnumber')->from('sz_betorder')->where("username='".$_SESSION['username']."'")->setPaging($data['pagesize'])->page($data['page'])->orderByASC(array('id'),false)->query();
        $ordercount = $this->db->select('count(*) as count')->from('sz_betorder')->where("username='".$_SESSION['username']."'")->row();
        $msg = [
            'method' => 'BetRecord',
            'count' => $ordercount['count'],
            'page' => $data['page'],
            'data' => $order,
        ];
        self::SendMsg(1001,$msg);
    }
    //兑奖测试
    public function returnaward($data,$client_id){
    /*计算时间开始*/
    $t1 = microtime(true);
    //执行程序
        $periodid = '201611010002';
        $orderlist = $this->db->select('*')->from('sz_betorder')->where("periodid='$periodid'")->orderByASC(array('id'),false)->query();
        foreach ($orderlist as $value){
            $awardnumber = '6,3,6';
            $winmoney = self::ExchangeAward($value['castcodes'],$awardnumber);
            $winmoney_sum +=$winmoney;
            if($winmoney == 0){
                $awardstatus = 1;
            }else{
                $awardstatus = 2;
            }
            //更新订单信息
            $sql = "UPDATE sz_betorder SET winmoney = $winmoney,awardstatus=$awardstatus,awardnumber ='$awardnumber'  WHERE orderid='".$value['orderid']."'";
            $this->db->query($sql);
            //更新用户金币
            $sql = "UPDATE sz_user SET gold =gold+$winmoney WHERE username='".$value['username']."'";
            $this->db->query($sql);
            $user_award_sum[$value['username']]['awardmoney'] += $winmoney;
        }
        //减掉奖池金
        $sql = "UPDATE sz_config SET awardpool = awardpool-$winmoney_sum WHERE channel='3721zh'";
        $this->db->query($sql);
        //更新期次返金总额
        $this->db->update('sz_period')->cols(array('awardsum'=>$winmoney_sum))->where("periodid='$periodid'")->query();
        print_r($user_award_sum);
        echo "------------------------\n";
        echo $winmoney_sum."\n";
    $t2 = microtime(true);
    echo '耗时'.round($t2-$t1,3)."秒\n";
    /*计算时间结束*/
    }
    public function test1($data) {
        global $playrules;
        //print_r($playrules[2]['value']);
        $period = $this->db->select('*')->from('sz_period')->where("status=0")->orderByASC(array('id'), false)->row();
        $period['periodid']='201611060007';
        $betorder = $this->db->select('*,sum(winmoney) as sumwinmoney')->from('sz_betorder')->where("periodid='".$period['periodid']."' and awardstatus>0")->groupBy(array('username'))->query();
        foreach ($betorder as $value){
            if(Gateway::isUidOnline($value['username']) == 1){
                $msg = [
                    'method' => 'Exchange',
                    'periodid' => $value['periodid'],
                    'winmoney' => $value['sumwinmoney'],
                ];
                Gateway::sendToUid($value['username'], json_encode(array('status'=>1001,'data'=>$msg,'msg'=>'成功')));
            }
        }
        
        print_r($betorder);

    }
    
    public function test(){
        $period = $this->db->select('*')->from('sz_period')->where("status=0")->orderByASC(array('id'), false)->row();
        //Core::CalcAwardNubmer($period);
    }
    public function t($str){
        $str = "1,2,3,4:4:6:100";
        self::CastCount($str);
    }
}